<template>
  <div>
    <r-search
      :searchForm="searchForm"
      :searchHandle="searchHandle"
      :searchData="searchData"
    />
    <r-table
      :tableHandles="tableHandles"
      :isSelection="false"
      :isIndex="false"
      :isPagination="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      :isHandle="true"
      @refresh="init()"
    >
    </r-table>
    <!-- Edit按钮打开的表单 -->
    <r-form
      :formRules="formRules"
      :formHandle="formHandle"
      :formData="formData"
      :formCols="formCols"
      ref="elForm"
      labelWidth="250px"
      dialogWidth="800px"
      :isHandle="true"
    />

    <Associated_devices
      :res="Associated_devices_data"
      v-if="Associated_devices_data.show"
      v-on:Associated_devices_call="Associated_devices_call"
    ></Associated_devices>
  </div>
</template>
<script>
import Associated_devices from "./cus/Associated_devices";
export default {
  components: {
    Associated_devices
  },
  data() {
    return {
      Associated_devices_data: {
        show: false,
        id: ""
      },
      searchData: {
        type: ""
      },
      searchForm: [
        {
          type: "select",
          prop: "type",
          placeholder: "PackageType",
          width: 150,
          options: [
            { label: "按时间收费", value: 1 },
            { label: "按电量收费", value: 2 }
          ]
        }
      ],
      searchHandle: [
        {
          label: "Search",
          type: "success",
          icon: "el-icon-search",
          handle: e => this.init()
        }
      ],

      tableHandles: [
        {
          label: "AddPackage",
          type: "primary",
          handle: e => this.elFormVisible()
        }
      ],
      tableData: [],
      tableCols: [
        {
          label: "序号",
          prop: "id"
        },
        {
          label: "充电Package名",
          prop: "mealName"
        },
        {
          label: "PackageType",
          prop: "type",
          formatter: row => {
            if (row.type == 1) {
              return "按时间收费";
            } else if (row.type == 2) {
              return "按电量收费";
            }
          }
        },
        {
          label: "收费标准",
          prop: "price"
        },
        {
          label: "关联设备",
          prop: "deviceNum"
        },
        {
          label: "Update Time",
          prop: "ctime"
        },
        {
          label: "EnableStatus",
          type: "switch",
          prop: "status",
          values: [1, 2],
          change: row => this.changestate(row)
        },

        {
          label: "Operations",
          type: "button",
          width: 300,
          btnList: [
            {
              label: "已关联设备",
              type: "primary",
              handle: row => {
                this.Associated_devices_data.id = row.id;
                this.Associated_devices_data.show = true;
              }
            },
            {
              label: "Edit",
              type: "primary",
              handle: row => this.elFormDetail(row)
            },
            {
              label: "Delete",
              type: "primary",
              handle: row => this.del(row)
            }
          ]
        }
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
      formData: {
        mealName: "",
        price: "",
        type: ""
      },
      formCols: [
        { type: "input", label: "Package Name", width: 350, prop: "mealName" },
        {
          type: "select",
          label: "PackageType",
          width: 350,
          prop: "type",
          options: [
            { label: "按时间收费", value: 1 },
            { label: "按电量收费", value: 2 }
          ]
        },
        {
          type: "input",
          label: "收费标准（每小时/每度电量）",
          width: 350,
          prop: "price",
          remark: "元"
        }
        // { type: "input", label: "Gift Amount", width: 350, prop: "giftMoney", remark: '元' },
        // { type: "switch", label: "上线/下线", width: 350, prop: "status" },
      ],
      formRules: {
        mealName: [
          { required: true, message: "Please Input", trigger: "blur" }
        ],
        price: [{ required: true, message: "Please Input", trigger: "blur" }],
        type: [{ required: true, message: "Please Select", trigger: "blur" }]
      },
      formHandle: [
        {
          label: "Cancel",
          handle: e => this.elFormVisible()
        },
        {
          label: "Confirm",
          type: "success",
          handle: e => this.elFormSubmit()
        }
      ]
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let parm = {
        ...this.searchData,
        ...{
          pageNum: this.tablePage.page,
          pageSize: this.tablePage.limit
        }
      };
      let res = await this.apis.mealCharge_list(parm);
      if (res) {
        this.tableData = res.rows || [];
        this.tablePage.total = res.total;
      }
    },
    async elFormSubmit() {
      this.$refs.elForm.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            const res = await this.apis.mealCharge_update({ ...this.formData });
          } else {
            const res = await this.apis.mealCharge_save({ ...this.formData });
          }

          this.init();
          this.elFormVisible();
        }
      });
    },
    del(row) {
      this.$confirm(`DeleteCurrent这条Data`, "Hint", {
        confirmButtonText: "Confirm",
        cancelButtonText: "Cancel",
        type: "warning"
      }).then(async () => {
        const res = await this.apis.mealCharge_del({
          id: row.id
        });

        this.init();

        this.$message({
          message: "Operations Success！",
          type: "success"
        });
      });
    },
    async changestate(row) {
      const res = await this.apis.mealCharge_update({
        id: row.id,
        status: row.status
      });
      this.$message({
        message: "Operations Success！",
        type: "success"
      });
    },
    // Edit 按钮调用的方法
    elFormDetail(row) {
      console.log(row);
      Object.keys(this.formData).forEach(key => {
        this.formData[key] = row[key];
      });
      this.formData.id = row.id;
      this.elFormVisible();
    },
    Associated_devices_call() {
      this.Associated_devices_data.show = false;
    }
  }
};
</script>
<style scoped lang="scss">
.el-icon-search {
  margin-right: 10px;
}

.Tip {
  margin-left: 20px;
}
</style>
